// packageH/installationSever/installationSeverAftersale/installationSeverAftersale.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: 0,

    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    another_name:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.order_id = options.order_id;
    this.getListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false
    });
  },

  getListData() {
    this.initData();
    let {
      order_id
    } = this.data;
    let json = {
      order_id
    };
    let urlStr = app.getNetAddresss('plugin.live-install.frontend.order.getRefundList');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          listData: res.data.data,
          another_name:res.data.another_name,
          networkLoading: true
        });
        wx.setNavigationBarTitle({
          title: res.data.another_name.plugin_name
        })
      }
    });
  },
  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let {
        order_id
      } = this.data;
      let json = {
        order_id,
        page
      };
      let urlStr = app.getNetAddresss('plugin.live-install.frontend.order.getRefundList');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let listData = this.data.listData.concat(res.data.data);
            this.setData({
              listData
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

  async handBtnClick(evt) {
    let {refund_id,btn,index} = evt.currentTarget.dataset;

    //有些按钮需要弹窗二次确认
    if(btn.code == 'cancelRefundApply' || btn.code=='finishRefundApply'){
      let confirmFlag = await app.confirm(`是否确定${btn.name}`);
      if(confirmFlag==false) return; 
    }

    let urlStr = app.getNetAddresss(btn.api);
    app._postNetWork({
      url: urlStr,
      data: {
        refund_id
      },
      success: (resdata) => {
        let res = resdata.data;
        app.tips(res.msg);
        if (res.result !== 1) return;
        let listData = this.data.listData;
        if (btn.code == 'cancelRefundApply') {
          listData.splice(index, 1);
          this.setData({
            listData
          });
        } else if (btn.code == 'finishRefundApply') {
          let state_name = 'listData[' + index + '].state_name';
          let button_group = 'listData[' + index + '].button_group';
          this.setData({
            [state_name]: '售后完成',
            [button_group]: []
          });
        }

      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  bindPreviewImage(evt){
    let {arr,src} = evt.currentTarget.dataset;
    wx.previewImage({
      urls: arr,
      current:src
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});